<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/15 0015
 * Time: 13:42
 */

namespace app\login\controller;




use app\lib\im\Sms;
use think\Controller;
use think\Session;

class Login extends Controller
{
    /**
     * @Notes:登陆界面
     * @Interface index
     * @author: Chenrui
     * @Time: 2018/9/15 0015   13:43
     */
    public function index(){
        return $this->fetch();
    }


    /**
     * @Notes:获取验证码
     * @Interface getCode
     * @return \think\response\Json
     * @author: Chenrui
     * @Time: 2018/9/15 0015   18:29
     */
    public function getCode(){
        $data=$this->request->post();
        $code=(string)rand(10000,99999);
        Session::set("code",$code);
        $res=(new Sms())->sendSms($data["mobile"],[$code,"2"]);
        if($res){
            if($res["result"]==0){//发送成功
                return json([
                    "result"  =>0,
                    "msg"   =>"success",
                    "code"  =>$code
                ]);
            }else{
                return json([
                    "result"      =>$res["result"],
                    "errmsg"    =>$res["errmsg"],
                    "code"      =>$code
                ]);
            }

        }else{//请求出错
            return json([
                "result"    =>-1,
                "errmsg"    =>"服务端错误"
            ]);
        }
    }

    /**
     * @Notes:登陆
     * @Interface login
     * @return \think\response\Json
     * @author: Chenrui
     * @Time: 2018/9/15 0015   19:47
     */
    public function login(){
        $data=$this->request->post();
        $code=Session::get("code");
        if($code==$data['code']){
            Session::set("mobile",$data["mobile"]);
            return json([
               "result"     =>0,
               "msg"        =>"login success"
            ]);
        }else{
            return json([
                "result"    =>1000,
                "errmsg"    =>"验证码错误"
            ]);
        }
    }

}